package com.sywb.chuangyebao.contract;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.TrainColumn;
import com.sywb.chuangyebao.bean.TrainVideo;
import com.sywb.chuangyebao.bean.TrainingAudio;
import com.sywb.chuangyebao.bean.TrainingCarousel;
import com.sywb.chuangyebao.bean.TrainingInfo;
import com.sywb.chuangyebao.library.view.RatioImageView;
import com.sywb.chuangyebao.utils.DateTimeUtils;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.ListCheckActivity;
import com.sywb.chuangyebao.view.TrainMediaDetailActivity;
import com.sywb.chuangyebao.view.WebActivity;
import com.sywb.chuangyebao.widget.GridSpacingItemDecoration;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemClickListener;
import org.bining.footstone.log.Logger;
import org.bining.footstone.utils.TimeUtils;
import org.bining.footstone.widget.PullToRefreshView;
import org.bining.footstone.widget.SlideView;

import java.util.ArrayList;
import java.util.List;

public interface TrainContract {

    interface View extends BaseRefreshContract.View {

    }

    class Presenter extends BaseRefreshContract.Presenter<View> {

        private SlideView appletBanner;
        private VideoAdapter videoAdapter;
        private SpecialAdapter specialAdapter;

        private List<TrainingCarousel> carouselList;// 轮播图
        private List<TrainVideo> hotList;// 视频直播推荐
        private List<TrainColumn> columnList;// 专题推荐
        private List<TrainingAudio> audioList;// 音频推荐


        private LinearLayout llHead;
        private LinearLayout llSpecialHead;

        @Override
        public void onStart() {
            super.onStart();
            pullToRefreshView = mView.getPullToRefreshView();
            pullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshView pullToRefreshView) {
                    trainingHome();
                }
            });

            android.view.View headView = mActivity.getLayoutInflater().inflate(R.layout.layout_train_index_head, null);
            appletBanner = headView.findViewById(R.id.applet_banner);
            llHead = headView.findViewById(R.id.ll_head);
            llSpecialHead = headView.findViewById(R.id.ll_special_head);
            RecyclerView hotRecycler = headView.findViewById(R.id.recycler_hot);
            headView.findViewById(R.id.tv_more).setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(android.view.View view) {
                    mView.advance(ListCheckActivity.class, mActivity.getString(R.string.train_video));
                }
            });

            headView.findViewById(R.id.tv_special_more).setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(android.view.View view) {
                    mView.advance(ListCheckActivity.class, mActivity.getString(R.string.special_name));
                }
            });

            List<Integer> images = new ArrayList<>();
            images.add(R.drawable.provider_bg);
            images.add(R.drawable.tutor_bg);
            images.add(R.drawable.project_number_bg);
            images.add(R.drawable.user_bg);
            appletBanner.setImageViews(images);
            appletBanner.invalidate();

            appletBanner.setOnItemChildClickListener(new SlideView.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(android.view.View view, int i) {
                    Logger.e("appletBanner:" + i);
                    if (carouselList != null && carouselList.size() > i) {
                        TrainingCarousel trainingCarousel = carouselList.get(i);
                        if ("live".equals(trainingCarousel.type)) {
                            mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.live), String.valueOf(trainingCarousel.type_id));
                        } else if ("audio".equals(trainingCarousel.type)) {
                            mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.audio_detail), String.valueOf(trainingCarousel.type_id));
                        } else if ("video".equals(trainingCarousel.type)) {
                            mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.video_detail), String.valueOf(trainingCarousel.type_id));
                        } else if ("column".equals(trainingCarousel.type)) {
                            mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.special_detail), String.valueOf(trainingCarousel.type_id));
                        } else if ("url".equals(trainingCarousel.type)) {
                            mView.advance(WebActivity.class, trainingCarousel.title, trainingCarousel.url);
                        }
                    }
                }
            });

            doSlideConflict();

            videoAdapter = new VideoAdapter(mActivity);
            hotRecycler.setAdapter(videoAdapter);
            hotRecycler.setLayoutManager(new GridLayoutManager(mActivity, 2));
//            videoAdapter.setHeaderView(R.layout.layout_train_head);
            GridSpacingItemDecoration recyclerDivider = new GridSpacingItemDecoration(mActivity.getResources().getDimensionPixelSize(R.dimen.dp_8), false);
            hotRecycler.addItemDecoration(recyclerDivider);
            hotRecycler.setNestedScrollingEnabled(false);
            videoAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(android.view.View view, int i) {
                    TrainVideo trainingHot = videoAdapter.getItem(i);
                    mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.video_detail), String.valueOf(trainingHot.re_comment_id));
//                    if ("live".equals(trainingHot.flag)) {
//                        mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.live), String.valueOf(trainingHot.re_comment_id));
//                    } else {
//
//                    }
                }
            });

            specialAdapter = new SpecialAdapter(mActivity);
            commonRecycler.setAdapter(specialAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
            commonRecycler.setNestedScrollingEnabled(false);
            specialAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(android.view.View view, int i) {
                    if (i == 0) {
                        return;
                    }

                    TrainColumn column = specialAdapter.getItem(i - 1);
                    mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.special_detail), String.valueOf(column.column_id));
                }
            });

            headView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
            specialAdapter.setHeaderView(headView);
            initData();
        }

        @Override
        public void initData() {
            super.initData();
            onStartAsync();
            trainingHome();
        }

        private void trainingHome() {
            HttpUtils.trainingHome(new HttpCallback<TrainingInfo>() {

                @Override
                public void onStart() {
                    super.onStart();
                    pullToRefreshView.refreshFinish(true);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    pullToRefreshView.refreshFinish(false);
                    onFinishAsync();
                }

                @Override
                public void onSuccess(TrainingInfo trainingInfo) {
                    if (trainingInfo.carousel != null && trainingInfo.carousel.size() > 0) {
                        carouselList = new ArrayList<>();
                        List<String> images = new ArrayList<>();
                        for (int i = 0; i < trainingInfo.carousel.size(); i++) {
                            TrainingCarousel trainingCarousel = trainingInfo.carousel.get(i);
                            carouselList.add(trainingCarousel);
                            images.add(trainingCarousel.thumb_pic);
                        }
                        appletBanner.setImageViewsToUrl(images, new SlideView.ImageLoader() {
                            @Override
                            public void displayImage(Context context, String s, ImageView imageView) {
                                GlideUtils.getNormalIamgeToUrl(mActivity, s, imageView, R.drawable.news_pic_default);
                            }
                        });

                        appletBanner.invalidate();
                    }
                    if (trainingInfo.re_comment != null && trainingInfo.re_comment.size() > 0) {
                        hotList = new ArrayList<>();
                        hotList.addAll(trainingInfo.re_comment);
                        videoAdapter.setDatas(hotList);
                    }
//                    mView.setNoData(videoAdapter.getDataCount() == 0);
                    llHead.setVisibility(videoAdapter.getDataCount() == 0 ? android.view.View.GONE : android.view.View.VISIBLE);

                    specialAdapter.clearDatas();
                    if (trainingInfo.column != null && trainingInfo.column.size() > 0) {
                        columnList = new ArrayList<>();
                        columnList.addAll(trainingInfo.column);
                        specialAdapter.setDatas(columnList);
                    }
//                    setSpecialHeaderView();
                    llSpecialHead.setVisibility(specialAdapter.getDataCount() == 0 ? android.view.View.GONE : android.view.View.VISIBLE);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }

            });
        }


        /**
         * 处理banner与refreshLayout的滑动冲突
         */
        private void doSlideConflict() {
            appletBanner.getViewPager().addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    // Log.e("homeBanner.onPageScrolled,position=" + position + ",positionOffset=" + positionOffset + ",positionOffsetPixels=" + positionOffsetPixels);
                    appletBanner.onPageScrolled(position, positionOffset, positionOffsetPixels);
                    pullToRefreshView.setRefreshEnable(positionOffset <= 0);
                }

                @Override
                public void onPageSelected(int position) {
                    // Log.e("homeBanner.onPageSelected,position=" + position);
                    appletBanner.onPageSelected(position);
                    pullToRefreshView.setRefreshEnable(true);
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                    // Log.e("homeBanner.onPageScrollStateChanged,state=" + state);
                    appletBanner.onPageScrollStateChanged(state);
                    pullToRefreshView.setRefreshEnable(true);
                }
            });
        }

    }


    class MenuAdapter extends BaseRecyclerAdapter<String> {

        private int images[] = {
                R.drawable.live_broadcast_fill, R.drawable.video_fill_l, R.drawable.audio_fre_fill, R.drawable.special_column_fill,
        };

        MenuAdapter(Activity activity) {
            super(activity, R.layout.item_train_menu);
        }

        @Override
        protected void setItemData(ViewHolderHelper viewHolderHelper, int position, String model) {
            viewHolderHelper.setText(R.id.tv_menu, model);
            viewHolderHelper.setImageResource(R.id.iv_menu, images[position]);
        }
    }

    class VideoAdapter extends BaseRecyclerAdapter<TrainVideo> {

        private Activity mActivity;

        VideoAdapter(Activity activity) {
            super(activity, R.layout.item_train_video);
            this.mActivity = activity;
        }

        @Override
        protected void setItemData(ViewHolderHelper viewHolderHelper, int position, TrainVideo data) {
            RatioImageView ivVideo = viewHolderHelper.getView(R.id.iv_video);
            GlideUtils.getNormalIamgeToUrl(mActivity, data.thumb_pic, ivVideo);
//            viewHolderHelper.setText(R.id.tv_video_type, data.flag.equals("live") ? "直播" : "视频");
            viewHolderHelper.setText(R.id.tv_video_name, data.title);
//            viewHolderHelper.setText(R.id.tv_video_hot, String.valueOf(data.count.hits));
        }
    }

    class SpecialAdapter extends BaseRecyclerAdapter<TrainColumn> {

        private Activity mActivity;

        SpecialAdapter(Activity activity) {
            super(activity, R.layout.item_train_special);
            this.mActivity = activity;
        }

        @Override
        protected void setItemData(ViewHolderHelper viewHolderHelper, int position, TrainColumn data) {
            viewHolderHelper.setVisibility(R.id.line_divider, position == getDataCount() ? android.view.View.GONE : android.view.View.VISIBLE);
            RatioImageView ivVideo = viewHolderHelper.getView(R.id.iv_special);
            GlideUtils.getNormalIamgeToUrl(mActivity, data.thumb_pic, ivVideo);
            viewHolderHelper.setText(R.id.tv_special_name, data.title);
            viewHolderHelper.setText(R.id.tv_special_text, data.des);
            viewHolderHelper.setText(R.id.tv_special_time, TimeUtils.ymd(data.update_time * 1000L));
        }
    }

    class AudioAdapter extends BaseRecyclerAdapter<TrainingAudio> {

        private Activity mActivity;

        AudioAdapter(Activity activity) {
            super(activity, R.layout.item_train_audio);
            this.mActivity = activity;
        }

        @Override
        protected void setItemData(ViewHolderHelper viewHolderHelper, int position, TrainingAudio data) {
            ImageView ivVideo = viewHolderHelper.getView(R.id.iv_audio);
            GlideUtils.getCircleImageToUrl(mActivity, data.media_logo, ivVideo);
            viewHolderHelper.setText(R.id.tv_audio_name, data.title);
            viewHolderHelper.setText(R.id.tv_audio_time, DateTimeUtils.getMediaTime(data.media_length));
            viewHolderHelper.setText(R.id.tv_audio_person, data.auther);
            viewHolderHelper.setText(R.id.tv_audio_where, data.live_level);
        }
    }

}

